﻿using System;
using System.Text.RegularExpressions;

namespace Toybox.NicoSharp
{
	/// <summary>
	/// Content Id クラス
	/// </summary>
	public class ContentId
	{

		#region [Static]

		private const string REGEX_ID_CHECK = @"(?<prefix>[a-z]{2}|)(?<digits>\d+)";

		#endregion [Static]


		#region Constructor

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="prefix"></param>
		/// <param name="digits"></param>
		public ContentId(string prefix, string digits)
		{
			if (digits == null)
				throw new ArgumentNullException("digits");

			this.Prefix = prefix;
			this.Digits = digits;
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="text"></param>
		public ContentId(string text)
		{
			if (string.IsNullOrEmpty(text))
				throw new ArgumentNullException("text");

			// URLの場合は最後
			string[] str = text.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
			text = str[str.Length - 1];

			Match match = Regex.Match(text, REGEX_ID_CHECK);
			if (match.Success)
			{
				this.Prefix = match.Groups["prefix"].Value;
				this.Digits = match.Groups["digits"].Value;
			}
			else
			{
				this._completeId = text;
			}
		}

		#endregion Constructor


		#region public Members

		/// <summary>
		/// 英字部分(2文字 あるいは なし)
		/// </summary>
		public string Prefix
		{
			get;
			protected set;
		}

		/// <summary>
		/// 数字部分
		/// </summary>
		public string Digits
		{
			get;
			protected set;
		}

		/// <summary>
		/// 英字+数字
		/// </summary>
		public string CompleteId
		{
			get { return this.Digits == null ? this._completeId : this.Prefix + this.Digits; }
		}
		private string _completeId;

		#endregion public Members


		#region Publice Methods

		/// <summary>
		/// CompleteIdを出力
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return this.CompleteId;
		}

		#endregion Publice Methods

	}
}